#include "rows.h"

#include <QVariant>

#include "common.h"
#include "row_text.h"


namespace BitWin
{


//---- Header Row class

HeaderRow::HeaderRow(const QString& title_):
    Row(title_, -1)
{}

QVariant HeaderRow::data(
    int role, const BitRange& range_, const PRowText&
) const
{
    if (BitwiseDataRole == role)
    {
        QVariant result;
        result.setValue<PRowText>(PRowText::RAMP->mid(range_));
        return result;
    }
    else
        return QVariant();
}

bool HeaderRow::operator==(const HeaderRow& rhs) const
{
    return Row::operator==(rhs);
}

QString HeaderRow::repr() const
{
    return QString(
        "title: \"%1\", "
        "data: \"1\", \"2\", .. \"INFINITY\""
    );
}


//---- FieldRow class

FieldRow::FieldRow(
    const QString& title_, const BitRange& range_, const int word_no_
):
    Row(title_, word_no_),
    _range(range_)
{}

QVariant FieldRow::data(
    int role, const BitRange& range_, const PRowText& data_
) const
{
    if (BitwiseDataRole == role)
    {
        QVariant result;
        result.setValue<PRowText>(
            data_->viewIn(_range)->mid(range_)
        );
        return result;
    }
    else
        return QVariant();
}

bool FieldRow::operator==(const FieldRow& rhs) const
{
    return (
        (rhs._range == this->_range) &&
        Row::operator==(rhs)
    );
}

QString FieldRow::repr() const
{
    return QString("title: \"%1\", range: %2, word no: %3")
        .arg(title())
        .arg(_range.repr())
        .arg(wordNo())
    ;
}


//---- WordRow class

WordRow::WordRow(const QString& title_, const int word_no_):
    Row(title_, word_no_)
{}

QVariant WordRow::data(
    int role, const BitRange& range_, const PRowText& data_
) const
{
    if (BitwiseDataRole == role)
    {
        QVariant result;
        result.setValue<PRowText>(data_->mid(range_));
        return result;
    }
    else
        return QVariant();
}


} // BitWin namespace end
